<?php
namespace app\index\controller;

use app\index\model\CameraManager;
use think\Loader;

class Camera
{
    private $camera;
    public function __construct()
    {
        $this->camera = new CameraManager();
    }

    public function index()
    {
        $farm_id = $_GET["farm_id"];
        if(isset($farm_id)){
            echoJson(1,"读取成功",$this->camera->readCameraByFarm($farm_id));
        }

        echoJson(1,"读取成功",$this->camera->readAllCamera());
    }

    public function read($id){
        echoJson(1,"读取成功",$this->camera->readCamera($id));
    }

    public function create(){
        $validate = Loader::validate('CreateCamera');
        if(!$validate->check($_GET)){
            if($validate->getError() !== true){
                echoJson(0, $validate->getError());
            }
        }

        echoJson(1,"添加成功",$this->camera->createCamera($_GET));
    }

    public function delete($id){
        echoJson(1,"删除成功",$this->camera->deleteCamera($id));
    }

    public function update($id){
        echoJson(1,"更新成功",$this->camera->updateCamera($id,$_GET));
    }
}
